package com.xinghou.platform.dao.mange.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;
import com.xinghou.platform.dao.bases.JdbcTemplate;
import com.xinghou.platform.dao.bases.database.CustomerContextHolder;
import com.xinghou.platform.dao.mange.UserInfoDAO;
import com.xinghou.platform.model.main.UserInfo;

/**
 * 
 * @author wlw
 * 
 */
@Repository
public class UserInfoDAOImpl implements UserInfoDAO {
	@Resource
	private JdbcTemplate jdbcTemplate;

	public UserInfo getUserInfoById(Integer uid) throws DataAccessException {
		CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_READ);
		String sql = "SELECT u2.Info_id,u2.Nick_name,u2.Birthday,u2.Signature,u2.Head_id,u2.Create_time,u2.Update_time,u2.Reg_status FROM user u1,user_info u2 WHERE u1.Info_id=u2.Info_id AND u1.User_id=?";
		UserInfo userinfo = jdbcTemplate.queryForObject(sql,new Object[] { uid }, new UserInfoMapper());
		CustomerContextHolder.clearCustomerType();
		return userinfo;
	}	
	
	public List<String> getHeadIdByInfoId(Integer infoid)throws DataAccessException {
		CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_READ);
		String sql = "SELECT Head_id FROM user_info  WHERE Info_id=?";
		List<String> userInfo = jdbcTemplate.query(sql,new Object[] { infoid }, new StringMapper());
		CustomerContextHolder.clearCustomerType();
		return userInfo;
	}
	private static final class StringMapper implements RowMapper<String> {
		public String mapRow(ResultSet rs, int rowNum) throws SQLException {
			return rs.getString(1);
		}
	}
	
	private static final class UserInfoMapper implements RowMapper<UserInfo> {
		public UserInfo mapRow(ResultSet rs, int rowNum) throws SQLException {
			UserInfo userinfo = new UserInfo();
			userinfo.setInfoId(rs.getInt("Info_id"));
			userinfo.setNickName(rs.getString("Nick_name"));
			userinfo.setBirthday(rs.getTimestamp("Birthday"));
			userinfo.setSignature(rs.getString("Signature"));
			userinfo.setHeadId(rs.getInt("Head_id"));
			//userinfo.setCreateTime(rs.getTimestamp("Create_time"));
			//userinfo.setUpdateTime(rs.getTimestamp("Update_time"));
			userinfo.setRegStatus(rs.getInt("Reg_status"));
			return userinfo;
		}
	}

}
